package zcw.com.lib_leet_code;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 朱城委 on 2022/1/27.<br><br>
 *
 * 中等：组合总和 III
 */
public class Topic216 {

    public static void main(String[] args) {
        Topic216 instance = new Topic216();

        System.out.println(instance.combinationSum3(3, 7));
        System.out.println(instance.combinationSum3(3, 9));
    }

    public List<List<Integer>> combinationSum3(int k, int n) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> list = new ArrayList<>();

        helper(k, n, 1, result, list);
        return result;
    }

    private void helper(int k, int n, int start, List<List<Integer>> result, List<Integer> list) {
        if(k < 0) {
            return ;
        }

        if(k == 0 && n == 0) {
            result.add(new ArrayList<>(list));
            return ;
        }

        for(int i = start; i <= 9; i++) {
            list.add(i);
            if(n - i >= 0) {
                helper(k - 1, n - i, i + 1, result, list);
            }
            list.remove(list.size() - 1);
        }
    }
}
